﻿using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System;

namespace PackToKindle
{
    [DataContract]
    internal class Settings
    {
        public Settings()
        {
            MailSettings = new MailSettings();
        }

        [DataMember]
        public MailSettings MailSettings { get; set; }

        public static Settings ApplicationSettings
        {
            get
            {
                if (_settings == null)
                {
                    var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGS_FILE_NAME);
                    _settings = _LoadSettings(settingsPath);
                }

                return _settings;
            }
        }

        private static Settings _LoadSettings(string filePath)
        {
            Debug.Assert(filePath != null);

            var content = File.ReadAllText(filePath);
            return _Deserialize<Settings>(content);
        }

        private static T _Deserialize<T>(string text)
        {
            Debug.Assert(text != null);

            DataContractSerializer serializer = new DataContractSerializer(typeof(T));

            T obj = default(T);
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
            {
                obj = (T)serializer.ReadObject(ms);
            }

            return obj;
        }

        private const string SETTINGS_FILE_NAME = @"settings.xml";

        private static Settings _settings;
    }

    /// <summary>
    /// Class providing mail settings.
    /// </summary>
    [DataContract]
    public class MailSettings : ICredentialsByHost
    {

        #region ICredentialsByHost members

        public NetworkCredential GetCredential(string host, int port, string authenticationType)
        {
            return new NetworkCredential(Username, Password);
        }

        #endregion

        #region Pubic Properties

        //[DataMember]
        //public int MaxAttachSizeMB { get; private set; }

        //[DataMember]
        //public string SendToKindleAddress { get; private set; }

        //[DataMember]
        //public string MailAddress { get; private set; }

        //[DataMember]
        //public string Username { get; private set; }

        //[DataMember]
        //public string Password { get; private set; }

        //[DataMember]
        //public int SmtpPort { get; private set; }

        //[DataMember]
        //public string SmtpServer { get; private set; }

        //[DataMember]
        //public bool SSL { get; private set; }

        [DataMember(Order = 0)]
        public int MaxAttachSizeMB { get; private set; }

        [DataMember(Order = 1)]
        public string SendToKindleAddress { get; private set; }

        [DataMember(Order = 2)]
        public string MailAddress { get; private set; }

        [DataMember(Order = 3)]
        public string Username { get; private set; }

        [DataMember(Order = 4)]
        public string Password { get; private set; }

        [DataMember(Order = 5)]
        public int SmtpPort { get; private set; }

        [DataMember(Order = 6)]
        public string SmtpServer { get; private set; }

        [DataMember(Order = 7)]
        public bool SSL { get; private set; }

        #endregion

        #region Protected constants

        protected const int POP3_DEFAULT_DEFAULT_PORT = 110;
        protected const int IMAP_DEFAULT_PORT = 143;
        protected const int SMTP_DEFAULT_PORT = 25;

        #endregion
    }

}
